﻿var http_request = false;

function makerequest(act, view, secs, type, divtype) {
    var pars = '?ajax=1&';
    var tspan;
    http_request = false;
    numon = 0;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      alert('Error: Cannot create an XMLHTTP instance (AJAX)');
      return false;
    }
    if(type == 'eventcats') {
      for(var i=0;i<eventcats.length;i++) {
        if(document.getElementById(eventcats[i]).checked) {
          pars += eventcats[i]+'=on&';
          numon++;
        } else {
          pars += eventcats[i]+'=off&';
        }
      }
      if(numon == eventcats.length) {
        document.getElementById('eventall').checked = true;
      } else {
        document.getElementById('eventall').checked = false;
      }
      if(document.getElementById('eventall').checked) {
        eventall = 'eventall=all&';
      } else {
        eventall = '';
      }
      if(divtype == 'expanded') {
        var divtoreplace = 'listevents';
      } else if(divtype == 'list') {
        var divtoreplace = 'listevents';      
      } else {
        var divtoreplace = 'ajaxdiv';
      }
      http_request.onreadystatechange = function() {
          if (http_request.readyState == 4) {
              if (http_request.status == 200) {
                  document.getElementById(divtoreplace).innerHTML = http_request.responseText;
              } else {
                  document.getElementById(divtoreplace).innerHTML = 'Error connecting to server';
              }
          } else {
              document.getElementById(divtoreplace).innerHTML = '<div class="ajaxload"><p>Loading...</p></div>';
          }
      }
      pars += act+'=1&events=1&'+eventall+'view='+view+'&secs='+secs+'&divtype='+divtype;
      http_request.open('GET', url+pars, true);
      http_request.send(null);
   } else if((type == 'todocats') || (type == 'todospan')) {
      for(var i=0;i<todocats.length;i++) {
        if(document.getElementById(todocats[i]).checked) {
          pars += todocats[i]+'=on&';
          numon++;
        } else {
          pars += todocats[i]+'=off&';
        }
      }
      if(numon == todocats.length) {
        document.getElementById('todoall').checked = true;
      } else {
        document.getElementById('todoall').checked = false;
      }
      if(document.getElementById('todoall').checked) {
        todoall = 'todoall=all&';
      } else {
        todoall = '';
      }
      var spanform = document.forms['spanform'];
      if(spanform.todospan[0].checked) {
        tspan = spanform.todospan[0].value;
      } else if(spanform.todospan[1].checked) {
        tspan = spanform.todospan[1].value;
      } else if(spanform.todospan[2].checked) {
        tspan = spanform.todospan[2].value;
      } else if(spanform.todospan[3].checked) {
        tspan = spanform.todospan[3].value;
      } else if(spanform.todospan[4].checked) {
        tspan = spanform.todospan[4].value;
      }
      if(document.getElementById('includecompleted').checked) {
        includecompleted = '1';
      } else {
        includecompleted = '0';
      }
      if(divtype == 'main') {
        if(document.getElementById('taskstodo').innerHTML == '') {
          var newid = 'taskstodo';
          var oldid = 'taskstodo1';
        } else {
          var newid = 'taskstodo1';
          var oldid = 'taskstodo'
        }
        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    if(document.getElementById('taskstodo1') == null) {
                      document.getElementById('taskscontainer').innerHTML += '<div style="display: none; height: auto;" id="taskstodo1"></div>';
                    }
                    document.getElementById(newid).innerHTML = http_request.responseText;
                    new Effect.PhaseOut(oldid, {duration:0.8} );
                    new Effect.PhaseIn(newid, {duration:0.8} );
                    setTimeout('killoldid()',1000);
                } else {
                    document.getElementById(oldid).innerHTML = 'Error connecting to server';
                }
            }
        }
      } else if((divtype == 'expanded') || (divtype == 'list')) {
        http_request.onreadystatechange = function() {
          if (http_request.readyState == 4) {
              if (http_request.status == 200) {
                  document.getElementById('listtodo').innerHTML = http_request.responseText;
              } else {
                  document.getElementById('listtodo').innerHTML = 'Error connecting to server';
              }
          } else {
              document.getElementById('listtodo').innerHTML = '<div class="ajaxload"><p>Loading...</p></div>';
          }
        }
      }
   
      pars += act+'=1&todo=1&'+todoall+'secs='+secs+'&todospan='+tspan+'&includecompleted='+includecompleted+'&divtype='+divtype;
      http_request.open('GET', url+pars, true);
      http_request.send(null);   
   }
}
function getcustpanel(fromloc) {
    http_request = false;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      document.getElementById('customisepanel').innerHTML = 'Error: Cannot create an XMLHTTP instance (AJAX)';
      return false;
    }
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
	      document.getElementById('customisepanel').innerHTML = http_request.responseText;
	      document.getElementById('fromloc').value = fromloc;              
          } else {
              document.getElementById('customisepanel').innerHTML = 'Error connecting to server';
          }
      } else {
          document.getElementById('customisepanel').innerHTML = '<div class="ajaxload"><p>Loading...</p></div>';
      }    
    }
    http_request.open('GET', url+'?ajax=1&customisepanel=1', true);
    http_request.send(null);   
}
function doupdate(k1, v1, k2, v2, k3, v3, k4, v4) {
    http_request = false;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      document.getElementById(oldid).innerHTML = 'Error: Cannot create an XMLHTTP instance (AJAX)';
      return false;
    }
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
	      if(document.getElementById('taskstodo').innerHTML == '') {
	        var newid = 'taskstodo';
	        var oldid = 'taskstodo1';
	      } else {
	        var newid = 'taskstodo1';
	        var oldid = 'taskstodo'
	      }
	      if(document.getElementById('taskstodo1') == null) {
	        document.getElementById('taskscontainer').innerHTML += '<div style="display: none;" id="taskstodo1"></div>';
	      }
	      document.getElementById(newid).innerHTML = http_request.responseText;
     	      new Effect.PhaseOut(oldid, {duration:0.5} );
	      new Effect.PhaseIn(newid, {duration:0.5} );
	      setTimeout('killoldid()',1000);
//	      document.getElementById(oldid).parentNode.removeChild(document.getElementById(oldid));

          } else {
              document.getElementById(oldid).innerHTML = 'Error connecting to server';
          }
      }
    }
    http_request.open('GET', url+'?ajax=1&'+k1+'='+v1+'&'+k2+'='+v2+'&'+k3+'='+v3+'&'+k4+'='+v4, true);
    http_request.send(null);   
}
function showedit(id, update) {
    http_request = false;
    tododiv = document.getElementById('todo'+id);
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      tododiv.innerHTML = 'Error: Cannot create an XMLHTTP instance (AJAX)';
      return false;
    }
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
            editform = document.createElement('div');
            editform.id='edit'+id;
            editform.className = 'tododiv';
            editform.style.display = 'none';
            tododiv.parentNode.insertBefore(editform,tododiv.nextSibling);
    	    document.getElementById('edit'+id).innerHTML = http_request.responseText;
    	    window[id+'taskeditor'] = new tinymce.Editor(id+'task', {
            	theme : "advanced",
            	skin : "o2k7",
            	skin_variant: "silver",
            	plugins : "inlinepopups,emotions",
                theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,|,hr,removeformat,emotions",
                theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,cleanup,|,forecolor,backcolor",
                theme_advanced_buttons3 : "code",
            	theme_advanced_toolbar_location : "top",
            	theme_advanced_toolbar_align : "left",
            	theme_advanced_fonts : "Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Times New Roman=times new roman",
            	theme_advanced_font_sizes : "1,3,5",
            	theme_advanced_more_colors : false,
                valid_elements : "a[href|target=_blank],strong/b,em/i,br,span[style],p,ol,ul,li,img[src|alt|title|class]",
            	button_tile_map : true,
            	cleanup : true
            });
            window[id+'taskeditor'].render();
//    	    tinyMCE.execCommand('mceAddControl', false, id+'task');
            Calendar.setup({inputField:id+"datedue",displayArea:"mousehookdatedue"+id,button:"mousehookdatedue"+id});
   	    new Effect.PhaseOut(tododiv, {duration:0.5} );
            new Effect.PhaseIn(editform, {duration:0.5} );
          } else {
              document.getElementById(oldid).innerHTML = 'Error connecting to server';
          }
      }
    }
    http_request.open('GET', url+'?ajax=1&todo=showedit&id='+id, true);
    http_request.send(null);
}
function delitem(id,type) {
    http_request = false;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      document.getElementById('id').innerHTML = 'Error connecting to server '+http_request.responseText;
    }
    new Effect.PhaseOut(id, {duration:0.5} );
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
              return false;
          } else {
              document.getElementById(id).innerHTML = 'Error connecting to server '+http_request.responseText;
          }
      }
    }
    http_request.open('GET', url+'?ajax=1&act=delete&id='+id+'&type='+type, true);
    http_request.send(null);
}
function massdelete() {
    http_request = false;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      tododiv.innerHTML = 'Error: Cannot create an XMLHTTP instance (AJAX)';
      return false;
    } else {
      if (window.XMLHttpRequest) { http_request_events = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request_events = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request_events = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
      if (window.XMLHttpRequest) { http_request_todo = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request_todo = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request_todo = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }    
    }
    var names = '';
    var t2 = 0;
    var numevents = 0;
    var numtodos = 0;
    var eventtable = document.getElementById('listtable');
    if(eventtable != null) {
      var inputseventtable = eventtable.getElementsByTagName('input');    
      for(var t = names.length;t < inputseventtable.length;t++) {
        if((inputseventtable[t2].type == "checkbox") && (inputseventtable[t2].checked == true)) {
          if(names.length == 0) {
            names += 'e'+inputseventtable[t2].value;
          } else {
            names += '-e'+inputseventtable[t2].value;
          }
          numevents++;
//	fadeTableRow('row'+inputseventtable[t2].value);
        }
        t2++;
      }
    }
    if(document.getElementById('todotable') != null) {
      var todotable = document.getElementById('todotable');
      var inputstodotable = todotable.getElementsByTagName('input');
      for(var t = names.length;t < inputstodotable.length;t++) {
        if((inputstodotable[t].type == "checkbox") && (inputstodotable[t].checked == true)) {
          if(names.length == 0) {
            names += 't'+inputstodotable[t].value;
          } else {
            names += '-t'+inputstodotable[t].value;
          }
          numtodos++;
//          fadeTableRow('row'+inputstodotable[t].value);
        }
      }
    }
      
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
	    if(numtodos > 0) {
	      http_request_todo.onreadystatechange = function() {
	        if (http_request_todo.readyState == 4) {
	          if (http_request_todo.status == 200) {
	            document.getElementById('listtodo').innerHTML = http_request_todo.responseText;
	          } else {
	            document.getElementById('listtodo').innerHTML = 'Error connecting to server';
	          }
	        }
	      }
	      http_request_todo.open('GET', url+'?ajax=1&updatecal=1&nochange=1&todo=1&divtype=list', true);
	      http_request_todo.send(null);
	    }
	    if(numevents > 0) {
	      http_request_events.onreadystatechange = function() {
	        if (http_request_events.readyState == 4) {
	          if (http_request_events.status == 200) {
	            document.getElementById('listevents').innerHTML = http_request_events.responseText;
	          } else {
	            document.getElementById('listevents').innerHTML = 'Error connecting to server';
	          }
	        }
	      }
	      http_request_events.open('GET', url+'?ajax=1&updatecal=1&nochange=1&events=1&divtype=list', true);
	      http_request_events.send(null);
	    }
	    return false;
          } else {
            alert('Error connecting to server '+http_request.responseText);
          }
      } else {
        if(numtodos > 0) {
          document.getElementById('listtodo').innerHTML = '<div class="ajaxload"><p>Loading...</p></div>';
        }
        if(numevents > 0) {
          document.getElementById('listevents').innerHTML = '<div class="ajaxload"><p>Loading...</p></div>';
        }
      }
    }
    http_request.open('GET', url+'?ajax=1&mass=delete&ids='+names, true);
    http_request.send(null);
}
function edittodo(taskid) {
  var valid = true;
  document.getElementById(taskid+'error_taskname').style.display = 'none';
    if(document.getElementById(taskid+'taskname').value == "") {
      document.getElementById(taskid+'error_taskname').innerHTML = JS_TASKNAME;
      document.getElementById(taskid+'error_taskname').style.display = 'block';
      valid = false;
    }
  document.getElementById(taskid+'error_datedue').style.display = 'none';
  if((document.getElementById(taskid+'datedue').value != "") && (!document.getElementById(taskid+'datedue').value.match(/^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/(0?[13578]|1?[02]))|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$/))) {
      document.getElementById(taskid+'error_datedue').innerHTML = JS_DATEDUE;
      document.getElementById(taskid+'error_datedue').style.display = 'block';
      valid = false;
  }
  if(valid == true) {        
    http_request = false;
    evtitle = document.getElementById(taskid+'taskname').value.replace(/ /g, '__SPC__\n');
    evdue = document.getElementById(taskid+'datedue').value;
    evtask = document.getElementById(taskid+'task').value.replace(/\n/g, '__NL__\n');
    evtask = evtask.replace(/ /g, '__SPC__\n');
    if((document.getElementById(taskid+'taskstatuspublic') && document.getElementById(taskid+'taskstatuspublic').checked) || document.getElementById(taskid+'taskstatus').value == 'Public'){
        evstatus = 'Public';
    } else {
        evstatus = 'Private';
    }
    evcategory = document.getElementById(taskid+'taskcategory').value;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
      tododiv.innerHTML = 'Error: Cannot create an XMLHTTP instance (AJAX)';
      return false;
    }
    http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
          if (http_request.status == 200) {
	      if(document.getElementById('taskstodo').innerHTML == '') {
	        var newid = 'taskstodo';
	        var oldid = 'taskstodo1';
	      } else {
	        var newid = 'taskstodo1';
	        var oldid = 'taskstodo'
	      }
	      if(document.getElementById('taskstodo1') == null) {
	        document.getElementById('taskscontainer').innerHTML += '<div style="display: none;" id="taskstodo1"></div>';
	      }
	      document.getElementById(newid).innerHTML = http_request.responseText;
     	      new Effect.PhaseOut(oldid, {duration:0.5} );
	      new Effect.PhaseIn(newid, {duration:0.5} );
	      setTimeout('killoldid()',1000);
	      return false;
          } else {
              tododiv.innerHTML = 'Error connecting to server';
          }
      }
    }
    http_request.open('GET', url+'?ajax=1&todo=edit&id='+taskid
                                +'&edittaskname='+evtitle
                                +'&edittaskdatedue='+evdue
                                +'&edittaskdesc='+escape(window[taskid+'taskeditor'].getContent())
                                +'&edittaskcategory='+evcategory
                                +'&edittaskstatus='+evstatus
                           , true);
    http_request.send(null);
    window[taskid+'taskeditor'].remove(); // remove the MCE editor
    return false;
  }
  alert(valid);
}
function killoldid() {
    if(document.getElementById('taskstodo').style.display == 'none') {
      document.getElementById('taskstodo').innerHTML = '';
    } else if(document.getElementById('taskstodo1').style.display == 'none') {
      document.getElementById('taskstodo1').innerHTML = '';
    }
}
function savepref(pref, prefval) {
    var pars = '?';
    http_request = false;
    if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
    if (!http_request) {
        alert('Error: Cannot create an XMLHTTP instance (AJAX)');
        return false;
    }

    pars += 'ajax=1&savepref=1&pref='+pref+'&val='+prefval;
    http_request.open('GET', url+pars, true);
    http_request.send(null);
}